﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.IO;

namespace Tal.CaptchaControl
{
    public partial class CaptchaControl
    {
        /// <summary>
        /// Gets or sets the time before deleting the image on the server
        /// </summary>
        [Bindable(false),
      Category("Behaviour"),
      Description("The time in minutes for deleting the captcha image."),
      Localizable(false),
       DefaultValue(20)]
        public int ImageDeleteTime
        {
            get
            {
                if (ViewState["ImageDeleteTime"] == null)
                {
                    if (HttpContext.Current.Session != null)
                    {

                        return HttpContext.Current.Session.Timeout;
                    }
                    else
                    {
                        return Config.ImageDeleteTime;
                    }
                }
                else
                {

                    return (int)ViewState["ImageDeleteTime"];
                }
            }
            set
            {
                if (HttpContext.Current.Session != null)
                {
                    HttpContext.Current.Session["captchaImageDeleteTime"] = value;
                }
                ViewState["ImageDeleteTime"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the font family
        /// </summary>
        [Bindable(false),
      Category("Behaviour"),
      Description("The font family to use in the captcha image."),
      Localizable(false),
       DefaultValue("Arial")]
        public string FontFamily
        {
            get
            {
                if (ViewState["FontFamily"] == null)
                {
                    return Config.FontFamily;
                }
                else
                {
                    return (string)ViewState["FontFamily"];
                }
            }
            set
            {
                if (HttpContext.Current.Session != null)
                {
                    HttpContext.Current.Session["captchaFontFamily"] = value;
                }
                ViewState["FontFamily"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the width of the captcha image
        /// </summary>
        [Bindable(false),
      Category("Appearance"),
      Description("The image width."),
      Localizable(false),
       DefaultValue(200)]
        public int ImageWidth
        {
            get
            {
                if (ViewState["ImageWidth"] == null)
                {
                    return Config.ImageWidth;
                }
                else
                {
                    return (int)ViewState["ImageWidth"];
                }
            }
            set
            {
                if (HttpContext.Current.Session != null)
                {
                    HttpContext.Current.Session["captchaImageWidth"] = value;
                }
                ViewState["ImageWidth"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the height of the captcha image
        /// </summary>
        [Bindable(false),
      Category("Appearance"),
      Description("The image height."),
      Localizable(false),
       DefaultValue(70)]
        public int ImageHeight
        {
            get
            {
                if (ViewState["ImageHeight"] == null)
                {
                    return Config.ImageHeight;
                }
                else
                {
                    return (int)ViewState["ImageHeight"];
                }
            }
            set
            {
                if (HttpContext.Current.Session != null)
                {
                    HttpContext.Current.Session["captchaImageHeight"] = value;
                }
                ViewState["ImageHeight"] = value;
            }
        }

        /// <summary>
        /// Generate the image from our text and write to disk
        /// </summary>
        private void WriteImage()
        {
            this.FileName = ImageManager.WriteImage(this.ImageText);

        }

     

        /// <summary>
        /// The text on the current image
        /// </summary>
        private string imageText;

        /// <summary>
        /// Gets the text on the current image
        /// </summary>
        public string ImageText
        {
            get
            {
                if (string.IsNullOrEmpty(imageText))
                {
                    try
                    {
                        imageText = Utils.GenerateFromType(this.TextGeneratorClass ?? Config.DefaultTextGenerator, this.CharactersTypes);
                    }
                    catch
                    {
                        TextGenerator txg = new SixCharsTextGenerator();
                        imageText = txg.Generate(this.CharactersTypes);
                    }

                }
                return imageText;
            }
        }

        private string FileName;


        /// <summary>
        /// Gets the full phisical path of the current image
        /// </summary>        
        private string FullPath
        {
            get
            {

                if (Path.IsPathRooted(ImagesPath))
                {
                    return Path.Combine(ImagesPath, FileName);
                }
                else
                {
                    return HttpContext.Current.Server.MapPath(Path.Combine(ImagesPath, FileName));
                }

            }
        }

        /// <summary>
        /// Gets the full phisical path of the images directory
        /// </summary>
        private string ImagesPhisicalPath
        {
            get
            {
                if (!Path.IsPathRooted(ImagesPath))
                {
                    return HttpContext.Current.Request.MapPath(ImagesPath);
                }
                else
                {
                    return ImagesPath;
                }
            }
        }

        /// <summary>
        /// Gets or sets the class used for generating the text
        /// </summary>
        [Bindable(false),
       Category("Behaviour"),
       Description("The class used for generating the random text."),
       Localizable(false),
       DefaultValue("Tal.CaptchaControl.SixCharsTextGenerator")]
        public string TextGeneratorClass
        {
            get
            {
                return (string)ViewState["TextGeneratorClass"] ?? Config.DefaultTextGenerator;

            }
            set
            {
                if (HttpContext.Current.Session != null)
                {
                    HttpContext.Current.Session["captchaTextGeneratorClass"] = value;
                }
                ViewState["TextGeneratorClass"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the class used for generating the text
        /// </summary>
        [Bindable(false),
       Category("Behaviour"),
       Description("The class used for generating the image."),
       Localizable(false),
       DefaultValue("Tal.CaptchaControl.GrayImageGenerator")]
        public string ImageGeneratorClass
        {
            get
            {
                return (string)ViewState["ImageGeneratorClass"] ?? Config.DefaultImageGenerator;

            }
            set
            {
                if (HttpContext.Current.Session != null)
                {
                    HttpContext.Current.Session["captchaImageGeneratorClass"] = value;
                }
                ViewState["ImageGeneratorClass"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the images directory relative path
        /// </summary>
        [Bindable(false),
       Category("Behaviour"),
       Description("The temp images directory relative path."),
       Localizable(false),
        DefaultValue("CaptchaImages")]
        public string ImagesPath
        {
            get
            {
                return (string)ViewState["ImagesPath"] ?? Config.CaptchaDirectory;

            }
            set
            {
                if (HttpContext.Current.Session != null)
                {
                    HttpContext.Current.Session["captchaImagesPath"] = value;
                }
                ViewState["ImagesPath"] = value;
            }
        }
        /// <summary>
        /// Gets or sets the text to display next to the TextBox
        /// </summary>
        [Bindable(true),
        Category("Appearance"),
        DefaultValue("Please enter the characters from the image"),
        Description("The text to display next to the TextBox."),
        Localizable(true)]
        public string Text
        {
            get
            {
                string s = (string)ViewState["Text"];
                return s ?? "Please enter the characters from the image";
            }
            set
            {
                ViewState["Text"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the length of the text in the image
        /// </summary>
        [Bindable(true),
        Category("Appearance"),
        DefaultValue(6),
        Description("The number of characters in the image."),
        Localizable(false)]
        public int NumberOfCharacters
        {
            get
            {
                if (ViewState["NumberOfCharacters"] == null)
                {
                    return 6;
                }
                else
                {
                    return (int)ViewState["NumberOfCharacters"];
                }
            }
            set
            {
                ViewState["NumberOfCharacters"] = value;
            }
        }



        /// <summary>
        /// Gets or sets the type of characters to display in the image
        /// </summary>
        [Bindable(true),
        Category("Appearance"),
        DefaultValue(Tal.CaptchaControl.TextGenerator.CharacterType.AlphaNumeric),
        Description("The type of characters to display in the image."),
        Localizable(false)]
        public TextGenerator.CharacterType CharactersTypes
        {
            get
            {
                if (ViewState["CharactersTypes"] == null)
                {
                    if (HttpContext.Current.Session != null)
                    {
                        HttpContext.Current.Session["captchaCharactersTypes"] = TextGenerator.CharacterType.AlphaNumeric;
                    }
                    return TextGenerator.CharacterType.AlphaNumeric;
                }
                else
                {
                    return (TextGenerator.CharacterType)ViewState["CharactersTypes"];
                }
            }
            set
            {
                if (HttpContext.Current.Session != null)
                {
                    HttpContext.Current.Session["captchaCharactersTypes"] = value;//for the ajax regenerate
                }
                ViewState["CharactersTypes"] = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether to ignore
        /// the case of the characters
        /// </summary>
        [Bindable(true),
       Category("Appearance"),
       DefaultValue(true),
       Description("A value indicating whether to ignore the case of the characters"),
       Localizable(false)]
        public bool IgnoreCase
        {
            get
            {
                if (ViewState["IgnoreCase"] == null)
                {
                    return true;
                }
                else
                {
                    return (bool)ViewState["IgnoreCase"];
                }
            }
            set
            {
                ViewState["IgnoreCase"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the text to display in the case
        /// that the characters entered did not match the image
        /// </summary>
        [Bindable(true),
       Category("Appearance"),
       DefaultValue("Please try again"),
       Description("The text to display in the case that the characters entered did not match the image."),
       Localizable(true)]
        public string ErrorMessage
        {
            get
            {
                if (ViewState["ErrorMessage"] == null)
                {
                    return "Please try again";
                }
                else
                {
                    return (string)ViewState["ErrorMessage"];
                }
            }
            set
            {
                ViewState["ErrorMessage"] = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether to show the
        /// regenerate image link
        /// </summary>
        [Bindable(true),
        Category("Appearance"),
        DefaultValue(false),
        Description("A value indicating whether to show the regenerate image link."),
        Localizable(false)]
        public bool EnableRegenarateImage
        {
            get
            {
                if (ViewState["EnableRegenarateImage"] == null)
                {
                    return false;
                }
                else
                {
                    return (bool)ViewState["EnableRegenarateImage"];
                }
            }
            set
            {
                ViewState["EnableRegenarateImage"] = value;
                if (value)
                {
                    CreateScript();
                }
            }
        }

        /// <summary>
        /// Gets or sets the text to display next to the 
        /// regenerate image link
        /// </summary>
        [Bindable(true),
        Category("Appearance"),
        DefaultValue("Regenerate"),
        Description("The text to display next to the regenerate image link."),
        Localizable(true)]
        public string RegenarateImageText
        {
            get
            {
                if (ViewState["RegenarateImageText"] == null)
                {
                    return "Regenerate";
                }
                else
                {
                    return (string)ViewState["RegenarateImageText"];
                }
            }
            set
            {
                ViewState["RegenarateImageText"] = value;
            }
        }

        /// <summary>
        /// Gets or sets the name of the page to handle
        /// ajax requests if case regeration of an image is enabled
        /// </summary>
        [Bindable(false),
      Category("Behaviour"),
      Description("The name of the page to handle ajax requests if case regeration of an image is enabled. This page will be created automatically."),
      Localizable(false),
       DefaultValue("CaptchaControlAjaxRequest.aspx")]
        public string RengerateScriptName
        {
            get
            {
                if (ViewState["RengerateScriptName"] == null)
                {
                    return "CaptchaControlAjaxRequest.aspx";
                }
                else
                {
                    return (string)ViewState["RengerateScriptName"];
                }
            }
            set
            {
                ViewState["RengerateScriptName"] = value;
            }
        }

    }
}
